package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.siteGuard.domain.SiteGuard;
import com.sjgtw.cloud.web.siteGuard.service.ISiteGuardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 重点地段看守管理Controller
 * 
 * @author lvyoufang
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/app/siteGuard")
@Api(tags = "6.2.3、APP重点地段看守管理")
public class AppSiteGuardController extends BaseController
{
    @Autowired
    private ISiteGuardService siteGuardService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询重点地段看守管理列表
     */
    @ApiOperation(value = "查询重点地段看守管理列表")
    @GetMapping("/list")
    public TableDataInfo list(SiteGuard siteGuard)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }

        List<Long> deptIds = new ArrayList<>();
        if (user != null) {
            AjaxResult ajaxResult = systemServerService.getSubDeptIdsByUserId(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    deptIds = (List<Long>) object;
                }
            }
        }
        siteGuard.setDeptIds(deptIds);

        startPage();
        List<SiteGuard> list = siteGuardService.selectSiteGuardList(siteGuard);
        return getDataTable(list);
    }

    /**
     * 获取重点地段看守管理详细信息
     */
    @ApiOperation(value = "获取重点地段看守管理详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(siteGuardService.selectSiteGuardById(id));
    }

    /**
     * 新增重点地段看守管理
     */
    @ApiOperation(value = "新增重点地段看守管理")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SiteGuard siteGuard)
    {
        return toAjax(siteGuardService.insertSiteGuard(siteGuard));
    }

    /**
     * 修改重点地段看守管理
     */
    @ApiOperation(value = "修改重点地段看守管理")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SiteGuard siteGuard)
    {
        return toAjax(siteGuardService.updateSiteGuard(siteGuard));
    }

    /**
     * 删除重点地段看守管理
     */
    @ApiOperation(value = "删除重点地段看守管理")
	@PostMapping("/remove")
    public AjaxResult remove(String[] ids)
    {
        return toAjax(siteGuardService.deleteSiteGuardByIds(ids));
    }
}
